/**
 * Created by Administrator on 2017-02-15.
 */
$(function () {
    var serverUrl = "http://192.168.59.3:8082";
    var photo = "http://192.168.59.2:7090/";
    var urlsVar = {
        //项目管理--项目基本资料
        "getAllProjectType": serverUrl + "/console/basedata/projectType/getAllProjectTypeList", //获取工程类型列表 yes
        "findUserListByRole": serverUrl + "/console/findUserListByRole",  //根据条件查询项目承包人和工人信息
        "findCompanyByRole": serverUrl + "/console/findCompanyByRole",     //根据条件查询公司信息
        "saveProjectInfo": serverUrl + "/console/KZT0690/saveProjectInfo",  //控制台存储工程项目

        "findProjectAllByCondition": serverUrl + "/console/KZT0302001/findProjectAllByCondition",  //按条件获取所有项目  yes
        "batchUpdateProjectCourse": serverUrl + "/console/batchUpdateProjectCourse",    //批量修改项目进程状态为完工
        "batchDeleteProjects": serverUrl + "/console/batchDeleteProjects",   //批量删除项目

        "saveTeam": serverUrl + "/console/KZT0700/saveTeam",   //保存班组
        "findWorkerByTeam": serverUrl + "/console/findWorkerByTeam", //根据班组id查询工人信息列表


        "findItemInfoByProjectCode": serverUrl + "/console/KZT0270/KZT0730/findItemInfoByProjectCode",  //项目的班组信息
        "getProjectInfoByCode": serverUrl + "/console/KZT0302002/getProjectInfoByCode/", //根据项目编号获取项目详情 yes
        "updateProjectInfo": serverUrl + "/console/KZT0271/KZT0740/updateProjectInfo", //更新项目

        "teamDetail": serverUrl + "/console/teamDetail/",  //班组信息详情
        "deleteTeam": serverUrl + "/console/deleteTeam/",  //删除班组信息
        "getTeamTypeInfoList": serverUrl + "/console/getTeamTypeInfoList?", //获得班组类别list集合分页
        "updateTeam": serverUrl + "/console/KZT0272/KZT0750/updateTeam",  //更新班组
        "deleteWorkerItemsRelation": serverUrl + "/console/deleteWorkerItemsRelation/",  //删除工人与班组关系
        "saveWorkItemsRelation": serverUrl + "/console/saveWorkItemsRelation",  //保存工人与班组的关系
        "workItemsRelationList": serverUrl + "/console/workItemsRelationList"    //批量保存工人与班组的关系
    };
    var accTypeVar = {
        "1": "主账号",
        "2": "子账号",
        "3": "其他"
    };
    var funGatherVar = {
        getRecordDate:function (date,type) {
            // 判断传入值date是否存在
            if(date == undefined){ return; }
            // 将date时间戳转换为标准时间
            var time = new Date(date);
            // 提取年月日
            var Y = time.getFullYear();
            var M = time.getMonth() + 1;
            var D = time.getDate();
            // 提取星期
            var W = time.getDay();
            // 提取时分秒
            var h = time.getHours();
            var m = time.getMinutes();
            var s = time.getSeconds();
            // 不足10的填充0
            M = M >= 10 ? M : "0" + M;
            D = D >= 10 ? D : "0" + D;
            h = h >= 10 ? h : "0" + h;
            m = m >= 10 ? m : "0" + m;
            s = s >= 10 ? s : "0" + s;
            // 输出时间格式
            if(type==1){
                var weekDay=["一","二","三","四","五","六","天"];
                return Y + '-' + M + '-' + D + "  星期" + weekDay[W] + " " + h + " : " + m + " : " + s; // YYYY/MM/DD  W  hh:mm:ss
            }else{
                if(type==2){
                    return Y + '-' + M + '-' + D + " " + h + " : " + m + " : " + s;                      // YYYY/MM/DD  hh:mm:ss
                }else{
                    return Y + '-' + M + '-' + D;                                                        // YYYY/MM/DD
                }
            }
        },
        slcCtnHideAndShow: function () {
            $(".hide_con").click(function () {
                var isHide = $(".selector_container").css("display") == "none";
                if (isHide == false) {
                    $(".selector_container").hide();
                    $(this).find("span").html("显示条件")
                        .next().attr("src", "../../../img/xiangxialan.png");
                } else {
                    $(".selector_container").show();
                    $(this).find("span").html("隐藏条件")
                        .next().attr("src", "../../../img/xiangshanglan.png");
                }
            })
        },
        selecDateFun: function () {
            $(".form-date").datetimepicker({
                language: "zh-CN",
                weekStart: 1,
                todayBtn: 1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                minView: 2,
                forceParse: 0,
                format: "yyyy-mm-dd"
            });
        },
        onlyMonthFun:function(){
            $(".form-date").datetimepicker({
                language:  "zh-CN",
                weekStart: 1,
                todayBtn:  0,
                autoclose: 1,
                todayHighlight: 1,
                startView: 3,
                minView: 3,
                forceParse: 1,
                format: "yyyy-mm"
            });
        },
        getSearchDataFun:function(){
            var str = location.search;
            str = str.slice(1).split('&');
            var json = {};
            for (var i = 0; i < str.length; i++){
                var a = str[i].split('=');
                json[a[0]] = decodeURI(a[1]);
            }
            return json;
        }
    };
    var userRoleVar = {};
    $.ajax({
        async:false,
        url: urlsVar.getRoleLists + "/1/50",
        type: "get",
        dataType: "json",
        success: function (data) {
            $(data.data.list).each(function () {
                userRoleVar[this.id] = this.roleName;
            })
        }
    });
    var teamTypeVar={};
    $.ajax({
        async :false,
        url:urlsVar.teamClassTable,
        type:"get",
        data:{pageNum:1,pageSize:50},
        dataType:"json",
        success:function(data){
            $(data.data.list).each(function(){
                teamTypeVar[this.id]=this.classname;
            })
        }
    });
    var workTypeVar={};
    $.ajax({
        async :false,
        url:urlsVar.workTypeClass,
        type:"get",
        data:{pageNum:1,pageSize:50},
        dataType:"json",
        success:function(data){
            $(data.data.list).each(function(){
                workTypeVar[this.id]=this.classname;
            })
        }
    });
    var tabFmtTypeVal={
        start:`<i class="start"></i><span>已启用</span>`,
        stop: `<i class="stop"></i><span>已停用</span>`,
        delBtn: `<input type="button" class="btn work_delBtn" value="删除">`,
        move:`<b class="move_up"></b>
      <b class="move_top"></b>
      <b class="move_down"></b>
      <b class="move_bottom"></b>`,
        addAndUpdate:`<div class="compile_btns">
                  <a href="javascript:void(0)" class="confirm_btn" id="confirmBtn">保存</a>
                  <a href="javascript:void(0)" class="cancel_btn" id="cancelBtn">取消</a>
                  </div>`
    };
    var configVar = {
        url: urlsVar,
        accType: accTypeVar,
        funGather: funGatherVar,
        userRole: userRoleVar,
        teamType:teamTypeVar,
        workType:workTypeVar,
        tabFmtType:tabFmtTypeVal
    };
    $("body").data("config", configVar);
});



function resHandler(res) {
    var $data = [];
    var needData = {};
    if (res.data != null) {
        if (res.data.list) {
            $data = res.data.list;
            needData.total = res.data.total;
        } else {
            if($.isArray(res.data)==true){
                $data=res.data
            }else{
                $data = [res.data]
            }
        }
        if(window.resDataFmt){
            resDataFmt($($data));
        }
        needData.rows = $data;
    } else {
        needData.rows = $data
    }
    return needData;
}

